
using System;
using System.Collections;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
//using Microsoft.VisualBasic.CompilerServices;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
namespace NCS_CS
{
	public class TimerExt3
	{
		// Events
		public event endAllTaskEventHandler endAllTask;
		public delegate void endAllTaskEventHandler();
		// As endAllTaskEventHandler
		public event taskTimerEventHandler taskTimer;
		public delegate void taskTimerEventHandler(int id);
		// As taskTimerEventHandler

		public TimerExt3()
		{
			this.t = new Timer();
            this.tasks = new Dictionary<string,Task>();//List<Task>();
		}

		public void addOneTaskBy(int id, int refid, int secondstostart, myAction a, List<Mcsy.Cmd> aid)
		{
			Task tmp = new Task();
			tmp.id = id;
			tmp.refid = refid;
			tmp.timestart = secondstostart;
			tmp.a = a;
			tmp.aid = aid;
			if ((tmp.refid != 0)) {
				string str1 = Convert.ToString(tmp.refid);
				tmp.refid = Convert.ToInt16(str1);
				tmp.timestart = (this.gettaskTimeByid(ref (str1)) + tmp.timestart);
				tmp.refid = 0;
			}
			this.tasks.Add( id.ToString(),tmp);//, null, null);
		}

		private void addTasks()
		{
		}
		public bool isRuning()
		{
			return (this.t.Enabled);
		}
		public void clearTasks()
		{
			this.t.Enabled = false;
			this.tasks.Clear();
		}

		private void delTheTask(string id)
		{
			this.tasks.Remove(id);
		}

        //protected override void TimerExt3()
        //{
        //    this.t = null;
        //    this.tasks = null;
        //    //base.Finalize();
        //}

		private int gettaskTimeByid(ref string id)
		{
			Task tmp = (Task)this.tasks[id];
			return tmp.timestart;
		}

		public void resumeIt()
		{
			this.t.Enabled = true;
		}

		public void start()
		{
			this.tcount = 0;
			this.t.Interval = 0x3e8;
			this.t.Enabled = true;
		}

		public void suspend()
		{
			this.t.Enabled = false;
		}

		private void t_Tick(object eventSender, EventArgs eventArgs)
		{
			IEnumerator var1 = null;
			this.tcount += 1;
			var1 = this.tasks.GetEnumerator();
			try {

				while (var1.MoveNext()) {
                    Task tmp = (Task)((System.Collections.Generic.KeyValuePair<string, NCS_CS.TimerExt3.Task>)var1.Current).Value;
					if ((this.tcount == tmp.timestart)) {
						this.delTheTask(Convert.ToString(Convert.ToInt32(tmp.id)));
						tmp.a.Invoke(tmp.aid);
						taskTimerEventHandler var2 = this.taskTimer;
						if (((var2 != null))) {
							var2.Invoke(tmp.id);
						}
						if ((this.tasks.Count == 0)) {
							this.t.Enabled = false;
							endAllTaskEventHandler var3 = this.endAllTask;
							if (((var3 != null))) {
								var3.Invoke();
							}
						}
                        break;
					}
				}
			} finally {
				if (var1 is IDisposable) {
					(var1 as IDisposable).Dispose();
				}
			}
		}


		// Properties
		private Timer t {
			[DebuggerNonUserCode()]
			get { return this._t; }
			[MethodImpl(MethodImplOptions.Synchronized), DebuggerNonUserCode()]
			set {
				EventHandler handler = new EventHandler(this.t_Tick);
				if (((this._t != null))) {
					this._t.Tick -= handler;
				}
				this._t = value;
				if (((this._t != null))) {
					this._t.Tick += handler;
				}
			}
		}


		// Fields
		private Timer _t;
        private Dictionary<string, Task> tasks;
		private int tcount;

        //private int tmCt;
		// Nested Types
		//Public Delegate Sub endAllTaskEventHandler()

		public delegate void myAction(List<Mcsy.Cmd > i);

		[StructLayout(LayoutKind.Sequential)]
		private struct Task
		{
			public string name;
			public int id;
			public int refid;
			public int timestart;
			public myAction a;
				//阀门动作集合
			public List<Mcsy.Cmd > aid;
		}
	}
}


